﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace UploadBlobToAzure
{
    public partial class DeleteStorageFromListForm : Form
    {
        field_validator fv = new field_validator();
        string selectedStorageAccount = string.Empty;
        public DeleteStorageFromListForm()
        {
            InitializeComponent();
            if (File.Exists("uploadblobtoazure.xml"))
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("uploadblobtoazure.xml");
                    XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");
                    if (!comboAcctName.Items.Contains("---Select A Storage Account To Remove---"))
                    {
                        comboAcctName.Items.Add("---Select A Storage Account To Remove---");
                    }
                    foreach (XmlNode xml in xmlnl)
                    {
                        XmlNode node = xml.SelectSingleNode("storageaccount");
                        comboAcctName.Items.Add(node.InnerText);
                    }
                    comboAcctName.Text = comboAcctName.Items[0].ToString();
                    comboAcctName.DropDownStyle = ComboBoxStyle.DropDownList;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Application.Exit();
                }
            }
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            string acctName;
            if (fv.testIfFieldEmpty(comboAcctName.Text) || (comboAcctName.Text == "---Select A Storage Account To Remove---"))
            {
                MessageBox.Show("Please select a account to delete from the drop down!!");
                this.DialogResult = DialogResult.None;
            }
            else if (comboAcctName.SelectedItem.ToString()==selectedStorageAccount)
            {
                MessageBox.Show("Cannot Delete The Account That You are Connected to, Please disconnect from the storage account and then try again");
                this.DialogResult = DialogResult.None;
            }
            else
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("uploadblobtoazure.xml");
                    XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");
                    if (xmlnl.Count == 1)
                    {
                        MessageBox.Show("Cannot delete the Only Storage Account Entry in the Config File!!");
                        this.DialogResult = DialogResult.Cancel;
                        this.Close();
                    }
                    else
                    {
                        foreach (XmlNode xml in xmlnl)
                        {
                            XmlNode node = xml.SelectSingleNode("storageaccount");
                            if (node.InnerText == comboAcctName.SelectedItem.ToString())
                            {
                                acctName = comboAcctName.SelectedItem.ToString();
                                //MessageBox.Show(acctName);
                                xml.ParentNode.RemoveChild(xml);
                                doc.Save("uploadblobtoazure.xml");
                                break;
                            }
                        }
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Application.Exit();
                }
            }
        }

        private void comboAcctName_Click(object sender, EventArgs e)
        {
            if (comboAcctName.Items.Contains("---Select A Storage Account To Remove---"))
            {
                comboAcctName.Items.Remove("---Select A Storage Account To Remove---");
            }
        } 
  
        public void setStorageAccountName(string accountName)
        {
            selectedStorageAccount = accountName;
        }

        
    }
}
